var util = require('../../utils/util')
Page({
  data: {
    menuArr: [{
        img: '/images/user/shop_center.png',
        text: '招商中心',
        url: '/pages/CBOapply/CBOapply',
        type: 1
      },
      {
        img: '/images/user/join.png',
        text: '商家入驻',
        soldier_text: '商家中心',
        url: '/pages/BusinessJoin/BusinessJoin',
        type: 3
      },
      {
        img: '/images/user/cose.png',
        text: '累计消费',
        url: '/pages/userpages/allcose/allcose',
        type: 1
      },
      {
        img: '/images/user/document.png',
        text: '我的收藏',
        url: "/pages/userpages/collectStore/collectStore",
        type: 1
      },
      {
        img: '/images/user/my_info.png',
        text: '我的资料',
        url: "/pages/userpages/myInfo/myInfo",
        type: 3
      },
      {
        img: '/images/user/aboutus.png',
        text: '关于我们',
        url: "/pages/userpages/aboutus/aboutus",
        type: 3
      },
      {
        img: '/images/user/customer.png',
        text: '联系客服',
        url: "/pages/userpages/customer/customer",
        type: 3
      },
    ],
    userInfo: '',
    integralInfo: ''
  },
  navPage(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    let url = this.data.menuArr[index].url;
    if (index === 4 && this.data.userInfo.is_soldier !== 1) {
      wx.showModal({
        title: '提示',
        cancelColor: '#BDBABE',
        confirmColor: "#FF325B",
        content: '您当前还不是会员，请前往申请拥军卡',
        confirmText: '前往申请',
        cancelText: '考虑一下',
        success: (res) => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/card/card',
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url,
      })
    }
  },

  // changeType() {
  //   if (this.data.userInfo.is_soldier == 2) {
  //     wx.setStorageSync('userType', 0)
  //     this.setData({
  //       'userInfo.is_soldier': 1
  //     })
  //   } else {
  //     wx.setStorageSync('userType', 1)
  //     this.setData({
  //       'userInfo.is_soldier': 2
  //     })
  //   }
  // },
  toShopOwner() {
    wx.showModal({
      title: '提示',
      cancelColor: '#BDBABE',
      confirmColor: "#FF325B",
      content: '是否切换到商家端',
      success: (res) => {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/ShopOwnerPages/login/login',
          })
        }
      }
    })
  },

  goExchange() {
    wx.switchTab({
      url: '/pages/exchange/exchange',
    })
  },

  getHeight(el) {
    //获取元素高度
    let query = wx.createSelectorQuery();
    query.select(el).boundingClientRect(rect => {
      let height = rect.height;
      this.setData({
        infoTop: height
      })
    }).exec();
  },

  getUserInfo() {
    //从服务器获取用户信息
    util.sendRequest('get/userinfo', 'GET').then(res => {
      // console.log(res)
      if (res.data.code === 0) {
        wx.setStorageSync('userInfo', res.data.result)
        this.setData({
          userInfo: wx.getStorageSync('userInfo'),
          createDate: wx.getStorageSync('userInfo').create_time.slice(0, 10),
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  getHoverHeight() {
    //重新计算自定义导航栏的高度
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight,
          navTop = menuButtonObject.top, //胶囊按钮与顶部的距离
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //导航高度
        getApp().globalData.navHeight = navHeight;
        getApp().globalData.navTop = navTop;
        getApp().globalData.windowHeight = res.windowHeight;
        getApp().globalData.hoverHeight = res.windowHeight - navHeight;
        this.setData({
          hoverHeight: res.windowHeight - navHeight,
          navHeight: navHeight,
        })
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  onLoad: function (options) {
    this.getUserInfo()
    //integralInfo 申请服务器数据成功后 才能点击
    this.getHeight(".head_hover")
    this.getHoverHeight()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})